﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zyg.SSO.Core.Business;
using Zyg.Utility.DBUtility;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class SqlProvider : DALProvider
    {
        #region App

        public override Zyg.SSO.Core.Business.App SelectApp(Guid id)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, id);
            SqlDataReader reader = db.ExecuteSPReader("usp_SelectApp");

            if (reader != null && !reader.IsClosed && reader.Read())
            {
                Business.App app = new App();

                if (!reader.IsDBNull(1)) app.Name = reader.GetString(1);
                if (!reader.IsDBNull(2)) app.Url = reader.GetString(2);
                if (!reader.IsDBNull(3)) app.IP = reader.GetString(3);
                if (!reader.IsDBNull(4)) app.Port = reader.GetInt32(4);
                if (!reader.IsDBNull(5)) app.DateCreated = reader.GetDateTime(5);
                if (!reader.IsDBNull(6)) app.PictureAddress = reader.GetString(6);
                if (!reader.IsDBNull(7)) app.LoginFile = reader.GetString(7);

                reader.Close();

                return app;
            }
            else
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();

                return null;
            }
        }

        public override void InsertApp(Zyg.SSO.Core.Business.App app)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, app.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, app.Name);
            db.AddParameter("@Url", SqlDbType.VarChar, app.Url);
            db.AddParameter("@IP", SqlDbType.VarChar, app.IP);
            db.AddParameter("@Port", SqlDbType.Int, app.Port);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, app.DateCreated);
            db.AddParameter("@PictureAddress", SqlDbType.VarChar, app.PictureAddress);
            db.AddParameter("@LoginFile", SqlDbType.VarChar, app.LoginFile);

            db.ExecuteSP("usp_InsertApp");
        }

        public override void DeleteApp(Zyg.SSO.Core.Business.App app)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, app.Id);

            db.ExecuteSP("usp_DeleteApp");
        }

        public override void UpdateApp(Zyg.SSO.Core.Business.App app)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, app.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, app.Name);
            db.AddParameter("@Url", SqlDbType.VarChar, app.Url);
            db.AddParameter("@IP", SqlDbType.VarChar, app.IP);
            db.AddParameter("@Port", SqlDbType.Int, app.Port);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, app.DateCreated);
            db.AddParameter("@PictureAddress", SqlDbType.VarChar, app.PictureAddress);
            db.AddParameter("@LoginFile", SqlDbType.VarChar, app.LoginFile);

            db.ExecuteSP("usp_UpdateApp");
        }

        public override IList<App> FillApps()
        {
            IList<App> results = new List<App>();

            SqlServerUtility db = new SqlServerUtility();

            SqlDataReader reader = db.ExecuteSPReader("usp_SelectAppsAll");

            if (reader != null)
            {
                while (reader.Read())
                {
                    App app = new App();

                    if (!reader.IsDBNull(0)) app.Id = reader.GetGuid(0);
                    if (!reader.IsDBNull(1)) app.Name = reader.GetString(1);
                    if (!reader.IsDBNull(2)) app.Url = reader.GetString(2);
                    if (!reader.IsDBNull(3)) app.IP = reader.GetString(3);
                    if (!reader.IsDBNull(4)) app.Port = reader.GetInt32(4);
                    if (!reader.IsDBNull(5)) app.DateCreated = reader.GetDateTime(5);
                    if (!reader.IsDBNull(6)) app.PictureAddress = reader.GetString(6);
                    if (!reader.IsDBNull(7)) app.LoginFile = reader.GetString(7);

                    app.MarkOld();

                    results.Add(app);
                }

                reader.Close();
            }

            return results;
        }

        #endregion


    }
}
